using System;
using System.Windows.Forms;
using WegasManagementSystem.BLL;
using WegasManagementSystem.Models;

namespace WegasManagementSystem.Forms
{
    /// <summary>
    /// 工资详情表单
    /// </summary>
    public partial class SalaryDetailForm : Form
    {
        private readonly SalaryBLL _salaryBLL;
        private SalaryInfo _salaryInfo;
        private int _empId;
        private string _yearMonth;

        /// <summary>
        /// 构造函数
        /// </summary>
        public SalaryDetailForm()
        {
            InitializeComponent();
            _salaryBLL = new SalaryBLL();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="empId">员工ID</param>
        /// <param name="yearMonth">年月(YYYY-MM格式)</param>
        public SalaryDetailForm(int empId, string yearMonth) : this()
        {
            _empId = empId;
            _yearMonth = yearMonth;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="salaryInfo">工资信息</param>
        public SalaryDetailForm(SalaryInfo salaryInfo) : this()
        {
            _salaryInfo = salaryInfo;
            _empId = salaryInfo.EmpId;
            _yearMonth = salaryInfo.YearMonth;
        }

        private void SalaryDetailForm_Load(object sender, EventArgs e)
        {
            try
            {
                // 加载工资信息
                LoadSalaryInfo();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载工资信息时发生错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }
        }

        private void LoadSalaryInfo()
        {
            // 验证参数
            if (_empId <= 0 || string.IsNullOrEmpty(_yearMonth))
            {
                throw new ArgumentException("员工ID或年月无效");
            }

            // 分解年月字符串
            string[] parts = _yearMonth.Split('-');
            if (parts.Length != 2)
            {
                throw new ArgumentException("年月格式不正确，应为YYYY-MM");
            }

            int year, month;
            if (!int.TryParse(parts[0], out year) || !int.TryParse(parts[1], out month))
            {
                throw new ArgumentException("年月格式不正确");
            }

            // 获取工资信息
            _salaryInfo = _salaryBLL.GetSalaryByEmployeeAndMonth(_empId, _yearMonth);
            if (_salaryInfo == null)
            {
                throw new Exception($"未找到员工ID {_empId} 在 {_yearMonth} 的工资信息");
            }

            // 显示工资信息
            DisplaySalaryInfo();
        }

        private void DisplaySalaryInfo()
        {
            // 设置表单标题
            this.Text = $"{_salaryInfo.EmpName} {_yearMonth} 工资详情";

            // 设置基本信息
            lblEmpName.Text = _salaryInfo.EmpName;
            lblDeptName.Text = _salaryInfo.DeptName;
            lblYearMonth.Text = _salaryInfo.YearMonth;
            
            // 设置收入项
            lblBaseSalary.Text = _salaryInfo.BaseSalary.ToString("N2");
            lblPositionAllowance.Text = _salaryInfo.PositionAllowance.ToString("N2");
            lblPerformanceBonus.Text = _salaryInfo.PerformanceBonus.ToString("N2");
            lblOvertimePay.Text = _salaryInfo.OvertimePay.ToString("N2");
            
            // 设置扣款项
            lblSocialInsurance.Text = _salaryInfo.SocialInsurance.ToString("N2");
            lblHousingFund.Text = _salaryInfo.HousingFund.ToString("N2");
            lblIncomeTax.Text = _salaryInfo.IncomeTax.ToString("N2");
            lblAbsenceDeduction.Text = _salaryInfo.AbsenceDeduction.ToString("N2");
            lblOtherDeductions.Text = _salaryInfo.OtherDeductions.ToString("N2");
            
            // 设置合计
            lblTotalIncome.Text = _salaryInfo.TotalIncome.ToString("N2");
            lblTotalDeduction.Text = _salaryInfo.TotalDeduction.ToString("N2");
            lblNetSalary.Text = _salaryInfo.NetSalary.ToString("N2");
            
            // 设置计算日期
            lblCalculateDate.Text = _salaryInfo.CalculateDate.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                // 导出工资单到PDF
                bool result = _salaryBLL.ExportEmployeeSalaryToPdf(_empId, _yearMonth);
                if (result)
                {
                    MessageBox.Show("工资单已成功导出为PDF文件", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("导出工资单失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"导出工资单时发生错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
} 